/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-02-8       金鹏       the first version
 */

#include "asr01.h"

static rt_thread_t asr01_handl = RT_NULL;                       /* 语音识别线程句柄*/
static short start_flag = 1;                                    /* 启动标志*/
static short communi_flag = 0;                                  /* 联络标志*/
static short way = 3;                                           /* 导航方式*/
extern struct CMN_UART uart_device[6];
extern int cm_switch;
extern rt_mq_t voice_recv_mq;
extern short len;                                               /* 语音唯一长度*/
extern short group;                                             /* 组别*/
extern short voice_lock;                                        /* 语音锁*/
extern short warn_flag;                                         /* 求助标志*/

/*
* ASR01_Entry
* @Param NULL
*
* Change Logs:
* Date           Author     Notes
* 2025-2-8     金鹏        语音识别线程
*/
void ASR01_Entry(void *parameter)
{
    char ch;
    char buff[200] = "";
    unsigned char bytes[100];
    unsigned char bytes_uni[100];
    short count = 0;

    while(1){
        /* 从串口读取一个字节的数据，没有读取到则等待接收信号量 */
        while (rt_device_read(uart_device[5].uart_serial, -1, &ch, 1) != 1)
        {
            /* 阻塞等待接收信号量，等到信号量后再次读取数据 */
            rt_sem_take(uart_device[5].uart_rx_sem, RT_WAITING_FOREVER);
        }

        if(ch != '\n'){
            buff[count++] = ch;
        }
        else{
            buff[count] = '\0';

            /* 转换前*/
            //rt_kprintf("asr01 = %s\n",buff);

            /* 转换启动 0x -> UTF-8*/
            hex_to_bytes(buff,bytes);

            buff[0] = '\0';
            count = 0;
            if(rt_strcmp("wait", bytes) == 0){
                rt_kprintf("please wait\n");
                //Json_Add(buff, "id", "1");
                //Json_Add(buff, "log", "wait");
                //Json_Sent("jp-topic", buff);
                if(start_flag == 1){
                    SYN6288_Say_UTF8("系统已经启动");
                    start_flag = 0;
                }
            }
            else if(rt_strcmp(".", bytes) != 0){
                if(rt_strstr(bytes,"网络")){
                    change_net();
                    Json_Add(buff, "id", "1");
                    Json_Add(buff, "log", "change");
                    Json_Sent("jp-topic", buff);
                    SYN6288_Say_UTF8("已经切换网络");
                }
                else if(rt_strstr(bytes,"求助")){
                    warn_flag = 1;
                    SYN6288_Say_UTF8("说明原因");
                }
                else if(rt_strstr(bytes,"联络")){
                    /* 上锁*/
//                    rt_timer_start(voice_timer);
//                    voice_lock = 1;
                    communi_flag = 1;

                    SYN6288_Say_UTF8("请说");
                }
                else if(rt_strstr(bytes,"设置")){
                    SYN6288_Say_UTF8("选择你的导航方式，步行、公交、自行车");
                    /* 上锁*/
                    rt_timer_start(voice_timer);
                    voice_lock = 1;
                }
                else if(rt_strstr(bytes,"公交")){
                    way = 2;
                    SYN6288_Say_UTF8("已经选择公交");
                }
                else if(rt_strstr(bytes, "步行")){
                    way = 3;
                    SYN6288_Say_UTF8("已经选择步行");
                }
                else if(rt_strstr(bytes, "自行车")){
                    way = 1;
                    SYN6288_Say_UTF8("已经选择自行车");
                }
                else if(rt_strstr(bytes,"组")){
                    group = convert_hanzi_numbers(bytes);
                    SYN6288_Say_UTF8("已经选择");
                }
                else{
                    if(voice_lock == 0){
                        if(communi_flag == 1){          /* 小组留言*/
                            Json_Add(buff, "id", "1");
                            Json_Add(buff,"grp",group);
                            Json_Add(buff, "log", bytes);
                            Json_Sent("jp-topic", buff);
                            communi_flag = 0;
                            SYN6288_Say_UTF8("已经发送");
                        }
                        else if(warn_flag == 1){        /* 求助留言*/
                            Json_Add(buff, "id", "1");
                            Json_Add(buff, "warn", bytes);
                            Json_Sent("jp-topic", buff);
                            warn_flag = 0;
                            SYN6288_Say_UTF8("已经发送");
                        }
                        else if(communi_flag == 0){     /* 地点信息*/
                            Json_Add(buff, "id", "1");
                            Json_Add(buff, "way", way);
                            Json_Add(buff, "voice", bytes);
                            Json_Sent("jp-topic", buff);
                        }
                        else{

                        }
                    }
                    else{
                        rt_kprintf("busy !!\n");
                    }
                    /* 语音LOG*/
                    //SYN6288_Say_UTF8(bytes);
                }

            }
            else{
                rt_kprintf("empty !!\n");
            }
        }
    }
}

/*
* voice_timeout
* @Param NULL
*
* Change Logs:
* Date           Author     Notes
* 2025-4-11     金鹏        语音识别线程
*/
static void voice_timeout(void *parameter)
{
    /* 释放语音锁*/
    voice_lock = 0;
    rt_kprintf("voice unlock\n");
}


/*
* ASR01_Test
* @Param NULL
*
* Change Logs:
* Date           Author     Notes
* 2025-2-8     金鹏        语音识别测试
*/
ENTRY ASR01_Test(void)
{
    /* 语音识别模块初始化*/
    if(CMN_Uart_Init("uart6") != RT_EOK){
        rt_kprintf("ASR01_Init failed\n");
        return -RT_ERROR;
    }else{
        rt_kprintf("ASR01_Init Successful\n");
    }

    /* 创建语音识别线程*/
    asr01_handl = rt_thread_create("asr01",
        ASR01_Entry,RT_NULL,
        ENTRY_STACK_SIZE,
        ENTRY_PRI,
        ENTRY_TICK);

    /* 创建语音定时器  单次定时器 */
    voice_timer = rt_timer_create("voice", voice_timeout,
                             RT_NULL, 18000,
                             RT_TIMER_FLAG_ONE_SHOT);

    /* LOG*/
    if(asr01_handl != RT_NULL){
        rt_kprintf("asr01 thread create successful!!\n");

        /* 启动语音识别线程*/
        rt_thread_startup(asr01_handl);
    }else{
        rt_kprintf("asr01 thread create failed!!\n");

        return -RT_ERROR;
    }
}
